/*
 *      helpdialog.cpp
 *
 *      Copyright 2011 David Vachulka <david@konstrukce-cad.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include "defs.h"
#include "helpdialog.h"
#include "config.h"
#include "i18n.h"
#include "icons.h"
#include "help.h"

FXDEFMAP(HelpDialog) HelpDialogMap[] = {
    FXMAPFUNC(SEL_KEYPRESS, 0, HelpDialog::onKeyPress),
    FXMAPFUNC(SEL_COMMAND, HelpDialog_VIEWLICENSE, HelpDialog::onViewLicense)
};

FXIMPLEMENT(HelpDialog, FXDialogBox, HelpDialogMap, ARRAYNUMBER(HelpDialogMap))

HelpDialog::HelpDialog(FXMainWindow *owner)
        : FXDialogBox(owner, _("Help"), DECOR_TITLE|DECOR_BORDER|DECOR_RESIZE, 0,0,0,0, 0,0,0,0, 0,0)
{
    m_licenseShowed = false;

    m_contents = new FXVerticalFrame(this, LAYOUT_SIDE_LEFT|LAYOUT_FILL_X|LAYOUT_FILL_Y, 0,0,0,0, 10,10,10,10, 0,0);
    m_textframe = new FXHorizontalFrame(m_contents,LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_SUNKEN|FRAME_THICK,0,0,0,0, 0,0,0,0);
    m_text = new FXText(m_textframe,NULL,0,TEXT_READONLY|TEXT_WORDWRAP|LAYOUT_FILL_X|LAYOUT_FILL_Y);
    m_text->setVisibleRows(35);
    m_text->setVisibleColumns(90);
    m_text->setText(HELP_TEXT);
    m_buttonframe = new FXHorizontalFrame(m_contents, LAYOUT_FILL_X|PACK_UNIFORM_WIDTH);
    m_buttonViewLicense  = new dxEXButton(m_buttonframe, _("View license"), 0, this, HelpDialog_VIEWLICENSE, FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT, 0,0,0,0, 10,10,2,2);
    m_buttonClose  = new dxEXButton(m_buttonframe, _("C&lose"), 0, this, FXDialogBox::ID_ACCEPT, BUTTON_INITIAL|BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT, 0,0,0,0, 10,10,2,2);
}

HelpDialog::~HelpDialog()
{
}

void HelpDialog::create()
{
    FXDialogBox::create();
}

long HelpDialog::onKeyPress(FXObject *sender, FXSelector sel, void *ptr)
{
    if(FXTopWindow::onKeyPress(sender,sel,ptr)) return 1;
    if(((FXEvent*)ptr)->code == KEY_Escape)
    {
        handle(this,FXSEL(SEL_COMMAND,ID_CANCEL),NULL);
        return 1;
    }
    return 0;
}

long HelpDialog::onViewLicense(FXObject*, FXSelector, void*)
{
    if(m_licenseShowed)
    {
        m_buttonViewLicense->setText(_("View license"));
        m_text->setText(HELP_TEXT);
    }
    else
    {
        m_buttonViewLicense->setText(_("Hide license"));
        if(FXStat::exists(DXIRC_DATADIR PATHSEPSTRING "COPYING"))
            loadFile(DXIRC_DATADIR PATHSEPSTRING "COPYING");
        else
            m_text->setText(_("Unable load COPYING file"));
    }
    m_licenseShowed = !m_licenseShowed;
    return 1;
}

// Load file thanks for xfe again
void HelpDialog::loadFile(const FXString& file)
{
    FXFile textfile(file,FXFile::Reading);
    FXint size, n, c, i, j;
    FXchar *txt;

    // Opened file?
    if(!textfile.isOpen())
    {
        return;
    }

    // Get file size
    size=textfile.size();

    // Make buffer to load file
    if(!FXMALLOC(&txt,FXchar,size))
    {
        return;
    }

    // Set wait cursor
    getApp()->beginWaitCursor();

    // Read the file
    n=textfile.readBlock(txt,size);
    if(n<0)
    {
        FXFREE(&txt);
        getApp()->endWaitCursor();
        return;
    }

    // Strip carriage returns
    for(i=j=0; j<n; j++)
    {
        c=txt[j];
        if(c!='\r')
            txt[i++]=c;
    }
    n=i;

    // Set text
    m_text->setText(txt,n);
    FXFREE(&txt);

    // Kill wait cursor
    getApp()->endWaitCursor();

    return;
}



